package com.mall.server.controller.pms;


import cn.hutool.core.convert.Convert;
import com.mall.model.domain.PmsProductAttribute;
import com.mall.model.vo.AttrInfoVo;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.server.service.pms.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService attributeService;

    //根据商品分类的id获取商品属性及属性分类
    @GetMapping("/attrInfo/{productCategoryId}")
    public CommonResult attrInfo(@PathVariable("productCategoryId") Integer productCategoryId) {

        List<AttrInfoVo> vos = attributeService.attrInfo(productCategoryId);
        return CommonResult.success(vos);
    }

    @PostMapping("/create")
    public CommonResult addAttribute(@RequestBody PmsProductAttribute attribute) {

        attributeService.addAttribute(attribute);
        return CommonResult.success(null);
    }

    @PostMapping("/delete")
    public CommonResult deleteByIds(@RequestParam Long[] ids) {

        attributeService.deleteByIds(ids);
        return CommonResult.success(null);
    }

    @GetMapping("/list/{cid}")
    public CommonResult selectByCid(@PathVariable("cid") Integer cid, Integer type,
                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {

        CommonPage<PmsProductAttribute> pageInfo = attributeService.selectByCid(cid, type, pageNum, pageSize);
        return CommonResult.success(pageInfo);
    }

    @PostMapping("/update/{id}")
    public CommonResult updateAttribute(@PathVariable("id") Integer id, @RequestBody PmsProductAttribute attribute) {

        attributeService.updateAttribute(Convert.toLong(id), attribute);
        return CommonResult.success(null);
    }

    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable("id") Integer id) {

        PmsProductAttribute attribute = attributeService.findById(Convert.toLong(id));
        return CommonResult.success(attribute);
    }

}
